package chat.client;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.JTextArea;

public class ClientFunction {
	
	private Socket client;
	
	private JTextArea display;
	
	ClientFunction(JTextArea display,Socket login)
	{
		this.display=display;
		client=login;
	}
	
	public boolean send(String message)
	{
		try {
			PrintWriter writer=new PrintWriter(client.getOutputStream());
			writer.println(message);
			writer.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	public void start()
	{
		new Thread(new CilentListener()).start();
	}
	
	private class CilentListener implements Runnable
	{
		Scanner scanner;
		CilentListener()
		{
			try {
				scanner=new Scanner(client.getInputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(true)
			{
				String temp=scanner.nextLine();
				if(temp.equals("zhe message is using to check if you are alive"))
				{
					
				}
				else
				{
					display.append(temp+"\n");
				}
			}
		}
		
	}
}
